<?php


namespace App\Services\Dao;


use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\MeetingTemplate;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Contract\LengthAwarePaginatorInterface;


class MeetingTplDao
{
    /**
     * note: 获取所有模板
     * @param int $userId
     * @param int $limit
     * created by: lhw at 8/1/2022 9:46 下午
     * @return LengthAwarePaginatorInterface
     */
    public function findAllByUserId(int $userId, int $limit = 10): LengthAwarePaginatorInterface
    {
        return MeetingTemplate::query()->where('user_id', '=', $userId)
            ->orderByDesc('id')->paginate($limit);
    }

    /**
     * note:获取单个模板信息
     * @param int $id
     * created by: lhw at 9/1/2022 2:00 下午
     * @return Builder|Model|object|null
     */
    public function findInfoById(int $id): ?MeetingTemplate
    {
        $model = MeetingTemplate::query()->where('id', '=', $id)->first();
        if (empty($model)) {
            throw new BusinessException(ErrorCode::MEETING_TPL_NOT_EXIST, ErrorCode::getMessage(ErrorCode::MEETING_TPL_NOT_EXIST));
        }
        return $model;
    }
}
